package com.onlyphoto.onlymall.mvp.view.fragment

import android.os.Build
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseFragment
import com.onlyphoto.onlymall.mvp.contract.CPersonalCenter
import com.onlyphoto.onlymall.mvp.presenter.PPersonalCenterImpl

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.bean.response.MyBean
import com.onlyphoto.onlymall.bean.response.UserInfoBean
import com.onlyphoto.onlymall.mvp.view.activity.MainActivity
import com.onlyphoto.onlymall.mvp.view.activity.SettingActivity
import com.onlyphoto.onlymall.mvp.view.binder.MyHeaderBinder
import com.onlyphoto.onlymall.mvp.view.binder.VerticalMenuBinder
import com.onlyphoto.onlymall.utils.ConfigUtil
import kotlinx.android.synthetic.main.fragment_my.*
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter
import qiu.niorgai.StatusBarCompat


class MyFragment : BaseFragment<PPersonalCenterImpl>(), CPersonalCenter.IVPersonalCenter {

    var adapter: MultiTypeAdapter? = null
    var layoutManager: LinearLayoutManager? = null
    var items: Items? = null
    var names = arrayListOf("我的售后", "我的收货地址", "我的优惠券", "常见问题")
    var images = arrayListOf(R.mipmap.after_sale, R.mipmap.area, R.mipmap.coupon, R.mipmap.question)

    var userInfo: UserInfoBean? = null

    override fun retry() {}

    override fun init() {
        initRv()
        initListener()
    }

    private fun initListener(){
        setting.setOnClickListener{
            (activity as MainActivity).startActivityWithLogin(SettingActivity::class.java)
        }
    }

    private fun initRv(){
        adapter = MultiTypeAdapter()
        layoutManager = LinearLayoutManager(activity)
        layoutManager?.orientation = RecyclerView.VERTICAL
        adapter?.register(UserInfoBean::class.java, MyHeaderBinder())
        adapter?.register(MyBean.MenuBean::class.java, VerticalMenuBinder())
        rv_per_center.adapter = adapter
        rv_per_center.layoutManager = layoutManager
        items = Items()

        val userInfo = UserInfoBean()
        items?.add(userInfo)

        for ((index, name) in names.withIndex()){
            val menu = MyBean.MenuBean(index, images[index], name)
            items?.add(menu)
        }
        adapter?.items = items!!

    }

    override fun getLayoutRes(): Int {
        return R.layout.fragment_my
    }

    override fun createPresenter() {
        mPresenter = PPersonalCenterImpl(mContext, this)
    }

    override fun showLoading() {}

    override fun hideLoading() {}

    companion object {

        fun newInstance(): MyFragment {
            val fragment = MyFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onResume() {
        super.onResume()
        userInfo = ConfigUtil.instance.userInfo

        if (userInfo != null) {
            items?.removeAt(0)
            items?.add(0, userInfo)
        } else {
            items?.removeAt(0)
            items?.add(0, UserInfoBean())
        }
        adapter?.notifyDataSetChanged()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                StatusBarCompat.setStatusBarColor(activity!!, ContextCompat.getColor(mContext, R.color.theme_color))
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                StatusBarCompat.setStatusBarColor(activity!!, ContextCompat.getColor(mContext, R.color.white))
                activity!!.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }
    }

}
